/**
 * @Title:  AttentionDynamicsActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2015-12-7 上午10:20:14 
 * @version:  V1.0 
 */

package artid.com.arttogoa.fragment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import artid.com.arttogoa.R;
import artid.com.arttogoa.db.DataKeeper;
import artid.com.arttogoa.glide.GlideUtil;
import artid.com.arttogoa.parser.RecordDetailList;
import artid.com.arttogoa.task.CallbackSky;
import artid.com.arttogoa.util.MD5;
import artid.com.arttogoa.util.NetUtils;
import com.android.volley.toolbox.ImageLoader;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author 熊彬
 * @data: 2015-12-7 上午10:20:14
 * @version: V1.0
 */
public class ArtImageFragment extends Fragment implements OnClickListener {
	private TextView mTextView;
	private ImageView mImageView;
	private RecordDetailList.Pointinformation data;
	private CallbackSky call;
	private ImageLoader mImageLoader;

	public ArtImageFragment(CallbackSky call) {
		this.call = call;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.viewpager_edit_item, null);
		mTextView = (TextView) v.findViewById(R.id.tv_edit);
		mImageView = (ImageView) v.findViewById(R.id.iv_img);
		mTextView.setOnClickListener(this);
		// adapter = new AuctionAdapter(getActivity());
		data = (RecordDetailList.Pointinformation) getArguments().getSerializable("data");
		if (data.getIndex() == 0) {
			new Thread(networkTask).start();
		} else {
			GlideUtil.setImage(getActivity(), data.getImgurl(),
					R.drawable.yspba_img_mor, R.drawable.yspba_img_mor,
					mImageView);
		}
		mImageView.setOnClickListener(this);
		// RequestQueue mQueue = Volley.newRequestQueue(getActivity());
		// mImageLoader = new ImageLoader(mQueue, new BitmapCache());
		// ImageListener listener = ImageLoader
		// .getImageListener(mImageView,
		// android.R.drawable.ic_menu_rotate,
		// android.R.drawable.ic_delete);
		// mImageLoader.get(data.imgurl, listener);
		// mImageView.setImageBitmap(getLoacalBitmap(data.getImage()));

		return v;
	}

	/**
	 * 加载本地图片 http://bbs.3gstdy.com
	 * 
	 * @param url
	 * @return
	 */
	public Bitmap getLoacalBitmap(String url) {
		try {
			FileInputStream fis = new FileInputStream(url);
			Bitmap bitmap = BitmapFactory.decodeStream(fis);
			return bitmap;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	String path;
	/**
	 * 网络操作相关的子线程
	 */
	Runnable networkTask = new Runnable() {

		@Override
		public void run() {
			// TODO
			// 在这里进行 http request.网络请求相关操作
			// Uri urlb = null;
			path = data.getImgurl();
			System.out.println("data=" + data.toString());
			// FileOutputStream outStream;
			try {
				String name = MD5.getMD5(path)+ path.substring(path.lastIndexOf("."));
				File file = new File(NetUtils.getSDPath(), name);
				if (!file.exists()) {
					// 从网络上获取图片
					URL url;
					try {
						url = new URL(path);

						HttpURLConnection conn = (HttpURLConnection) url
								.openConnection();
						conn.setConnectTimeout(5000);
						conn.setRequestMethod("GET");
						conn.setDoInput(true);
						if (conn.getResponseCode() == 200) {
							InputStream is = conn.getInputStream();
							FileOutputStream fos = new FileOutputStream(file);
							byte[] buffer = new byte[1024];
							int len = 0;
							while ((len = is.read(buffer)) != -1) {
								fos.write(buffer, 0, len);
							}
							is.close();
							fos.close();
							// 返回一个URI对象
							// return Uri.fromFile(file);
						}
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				String url = data.getImgurl();
				// item = (RecordDetailList) DataKeeper.readSerializable(
				// getActivity(), RecordDetailList.class);
				// System.out.println("提交前：" + item == null ? "wu" : "you");
				// item.getPointinformation()
				// .get(data.getIndex())
				// .setImgurl(
				// url + NetUtils.identification
				// + NetUtils.getSDPath() + "/" + name);
				// // System.out.println("提交后：" + item .toString());19920620
				// DataKeeper.keepSerializable(getActivity(), item);
				Message msg = new Message();
				msg.obj = NetUtils.getSDPath() + "/" + name;
				handler.sendMessage(msg);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	};
	private RecordDetailList item;
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			String path = (String) msg.obj;
			// NetUtils.getLocalImage(item
			// .getPointinformation().get(data.getIndex()).getImgurl(), true)
			GlideUtil.setImage(getActivity(), path, R.drawable.yspba_img_mor,
					R.drawable.yspba_img_mor, mImageView);
			// Bitmap b = getLoacalBitmap(path);
			// mImageView.setImageBitmap(b);
			try {
				// item获取可能会空
				item = (RecordDetailList) DataKeeper.readSerializable(
						getActivity(), RecordDetailList.class);
				System.out.println("提交前：" + item == null ? "wu" : "you");
				item.setComImageUrl(path);
				// System.out.println("提交后：" + item .toString());19920620
				DataKeeper.keepSerializable(getActivity(), item);
			} catch (Exception e) {
				// TODO: handle exception
			}
			// TODO
			// UI界面的更新等相关操作
		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.iv_img:
			call.callBack(0);
			break;

		default:
			break;
		}
	}
}
